package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.Basic.util.AjaxResult;
import cn.itsource.pethome.Basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工控制类
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 员工列表查询
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }
    /**
     * 根据条件查询员工列表
     * @return
     */
    @PatchMapping
    public PageList<Employee> loadAllEmployeeByQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.loadAllByQuery(employeeQuery);
    }

    /**
     * 增加员工
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addEmployee(@RequestBody Employee employee){
        try {
            employeeService.add(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"增加失败"+e.getMessage());
        }
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult deleteEmployee(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            employeeService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    @PostMapping
    public AjaxResult updateEmployee(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }

}
